'use strict';

app.controller('AppCtrl', ['$scope', '$window', '$rootScope',
    function($scope, $window, $rootScope) {
        $rootScope.getSgObj = function(key) {
            var obj = $window.sessionStorage.getItem(key);
            return JSON.parse(obj);
        }
        $rootScope.setSgObj = function(key, value) {
            return $window.sessionStorage.setItem(key, JSON.stringify(value));
        }
    }
]);

app.controller('LoginCtrl', ['$scope', '$window', '$rootScope', 'R',
    function($scope, $window, $rootScope, R) {

        $scope.login = function(){
            R.login({account: $scope.account, pwd: md5($scope.password)}).then(function(data){
                if (data.rtnCode == '0000000'){
                    $rootScope.setSgObj('LOGINUSER', data.bizData);
                    window.location.href = 'index.html';
                }else{
                    $scope.error = data.msg;
                }
            }); 
        } 
    }
]);